<template>
    <div>
        <div class="nav"></div>
        <div class="wrap">
            <div class="body">
                <div class="title">{{title}}</div>
                <div class="line"></div>
                <div class="list">
                    <img class="red" src="../../images/main/red.png" alt="">
                    <span class="txt">当前密码：</span>
                    <input type="password" @blur="curPwdBlur" v-model="odlPass" class="input" placeholder="请输入当前密码">
                    <div class="hint" v-if="check.curPwd && !odlPass">请先输入当前密码</div>
                </div>
                <div class="list">
                    <img class="red" src="../../images/main/red.png" alt="">
                    <span class="txt">新密码：</span>
                    <input type="password" @blur="newPwdBlur" v-model="newPass" class="input" placeholder="请输入新密码">
                    <div class="hint" v-if="check.newPwd && !newPass">请先输入新密码</div>
                </div>
                <div class="list">
                    <img class="red" src="../../images/main/red.png" alt="">
                    <span class="txt">确认新密码：</span>
                    <input type="password" @blur="confirmPwdBlur" v-model="rePass" class="input" placeholder="请输入新密码">
                    <div class="hint" v-if="check.confirmPwd && newPass && rePass && newPass != rePass">两次密码输入不一致，请重新输入</div>
                    <div class="hint" v-if="check.confirmPwd && !rePass">请先输入新密码</div>
                </div>
                <div class="save" @click="save()">
                    保存
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import axios from "axios";
import qs from "qs"; // 将请求转换成form 表单提交形式
export default {
  data() {
    return {
      title: '修改密码',
      check: {
        curPwd: false,
        newPwd: false,
        confirmPwd: false
      },
      staffInfo: {},
      newPass: "",
      rePass: "",
      odlPass: ""
    };
  },
  created(){
    if(this.$cookie.get('servicePwd') === 'yunlehui2019'){
      this.title = '首次登陆系统，请先修改初始密码'
    }
  },
  methods: {
    newPwdBlur() {
      this.check.newPwd = true;
    },
    curPwdBlur() {
      this.check.curPwd = true;
    },
    confirmPwdBlur() {
      this.check.confirmPwd = true;
    },
    save: function() {
      let that = this;
       let _this = this;
        if ( !this.odlPass ) {
            // this.$alert ( '请输入旧密码' );
            this.check.curPwd = true;
            return;
        } else if ( !this.newPass ) {
            this.check.newPwd = true;
            // this.$alert ( '请输入新密码' );
            return;
        } else if ( !this.rePass ) {
            this.check.confirmPwd = true;
            // this.$alert ( '请再次输入新密码' );
            return;
        } else if ( this.newPass != this.rePass ) {
            // this.$alert ( '两次输入的密码不一致' );
            this.check.confirmPwd = true;
            return;
        }
                if(this.$cookie.get('servicePwd') != this.odlPass) {
                    this.$alert('当前面密码有误，请重新输入！')
                    return
                }
        axios({
          method: "post",
          url: `/service/updatePwd`,
          data: qs.stringify({
            id: this.$store.state.userId,
            pwd: that.newPass
          })
        }).then(res => {
          if (res.data.code == 0) {
            that.$message({
               type: 'success',
               message: '修改成功!',
               duration: 1000,
               onClose: function(){
                  that.$cookie.set('servicePwd',that.newPass)
                  that.$router.push({
                    path: "/"
                  });
                  that.task = false;
               }
            })

          } else {
            alert(res.msg);
          }
        });
      // }
    }
  },
};
</script>
<style scoped>
.hint {
  color: red;
  margin-left: 150px;
}
.nav {
  background: #353948;
  /* width: 1920px; */
  height: 111px;
}
.wrap {
  width: 100%;
  height: clac(100vh - 211px);
  background: #eceef3;
  padding: 100px 0;
}
.body {
  background: #ffffff;
  box-shadow: 0 5px 20px 0 rgba(0, 0, 0, 0.05);
  border-radius: 6px;
  /* width: 1071px; */
  width: 90%;
  height: 658px;
  margin: 0 auto;
}
.title {
  height: 115px;
  font-family: PingFangSC-Medium;
  font-size: 24px;
  color: #000000;
  letter-spacing: 0;
  line-height: 115px;
  text-align: center;
  font-weight: bold;
}
.line {
  background: #f5f5f5;
  /* width: 861px; */
  height: 3px;
  margin: 0 auto;
}
.list {
  height: 40px;
  /* margin-top: 64px; */
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #000000;
  line-height: 40px;
  width: 500px;
  margin: 60px auto 0;
}

.red {
  width: 10px;
  height: 10px;
  /* margin-left: 285px; */
  /* margin-left: 30% */
}
.txt {
  width: 109px;
  display: inline-block;
  text-align: left;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  font-weight: bold;
  color: #000000;
}
.input {
  background: #ffffff;
  box-shadow: inset 0 1px 3px 0 rgba(0, 0, 0, 0.5);
  width: 307px;
  height: 40px;
  margin-left: 10px;
  padding-left: 10px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #888888;
  box-sizing: border-box;
}
.save {
  background: #085dab;
  width: 255px;
  height: 55px;
  text-align: center;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #ffffff;
  /* margin-left: 420px; */
  /* margin-top: 102px; */
  cursor: pointer;
  line-height: 55px;
  margin: 80px auto;
}
body{
  background-color: #eceef3;
}
</style>